<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>物流管理系统登录</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">

    <link rel="shortcut icon" href="img/favicon.ico" type="image/x-icon">
    <link href="layui/css/layui.css" rel="stylesheet">

    <style>
        body {
            background-image: url("img/background.jpg");
        }

        .login-container {
            width: 350px;
            margin: 50px auto 0;
            padding: 20px 50px;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

    </style>
</head>

<body>
<div style="margin: 120px auto 0;text-align: center"><h1 style="color: white">物流管理系统</h1></div>
<form class="layui-form login-container">
    <div style="text-align: center"><h3>账号密码登录</h3></div>
    <!-- 水平线 -->
    <hr class="layui-bg-blue">

    <form class="layui-form">
        <div class="layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-username"></i>
                </div>
                <input type="text" name="userName" value="admin" lay-verify="required" placeholder="用户名"
                       lay-reqtext="请填写用户名" autocomplete="off" class="layui-input" lay-affix="clear">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-password"></i>
                </div>
                <input type="password" name="password" value="123456" lay-verify="required" placeholder="密   码"
                       lay-reqtext="请填写密码" autocomplete="off" class="layui-input" lay-affix="eye">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-row">
                <div class="layui-col-xs7">
                    <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-vercode"></i>
                        </div>
                        <input type="text" name="captcha" value="" lay-verify="required" placeholder="验证码"
                               lay-reqtext="请填写验证码" autocomplete="off" class="layui-input" lay-affix="clear">
                    </div>
                </div>
                <div class="layui-col-xs5">
                    <div style="margin-left: 10px;text-align: center">
                        <img src="/captcha/generate" style="width: 88px;height: 36px;margin: auto"
                             onclick="this.src='/captcha/generate?t='+ new Date().getTime();">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <input type="checkbox" name="remember" lay-skin="primary" title="记住密码">
            <a href="#forget" style="float: right; margin-top: 7px;">忘记密码？</a>
        </div>
        <div class="layui-form-item">
            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-login">登录</button>
        </div>
    </form>
</form>

<script src="layui/layui.js" charset="utf-8"></script>
<script src="jquery/jquery-3.6.0.js" charset="utf-8"></script>
<script>
    layui.use(function () {
        var form = layui.form;
        var layer = layui.layer;
        // 提交事件
        form.on('submit(demo-login)', function (data) {
            var field = data.field;

            $.ajax({
                url: "/admin/login",           // 请求路径
                type: "post",                  // 请求的方式，不区分大小写
                cache: false,                  // 关闭缓存，目的是为了避免部分浏览器缓存加载出错(IE)
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    userName: field.userName,
                    password: field.password,
                    captcha: field.captcha
                }),
                datatype: "json",        // 返回类型，text文本、html页面、json数据
                success: function (response) {
                    console.log("返回: " + response);
                    if (response.code === 0) {
                        layer.msg('登陆成功', {icon: 1});
                        window.location.href = "layout.html";
                    } else if (response.code === 1) {
                        layer.msg("用户名或密码错误", {icon: 2});
                    }else{
                        layer.msg("验证码错误", {icon: 2});
                    }
                },
                error: function (response) {
                    layer.msg("网络异常", {icon: 2});
                }
            });

            return false; // 阻止默认 form 跳转
        });
    });
</script>
</body>

</html>
